package com.example.jetour_line;

import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.text.TextUtils;
import android.util.Log;

import androidx.annotation.NonNull;
import com.amap.api.location.AMapLocationClient;
import com.amap.api.maps.MapsInitializer;
import com.example.jetour_line.map.mapview.BaseLocationUtil;
import com.example.jetour_line.map.plugin.MyViewFlutterPlugin;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.flutter.embedding.engine.plugins.FlutterPlugin;
import io.flutter.plugin.common.EventChannel;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugin.common.MethodChannel.MethodCallHandler;
import io.flutter.plugin.common.MethodChannel.Result;

/**
 * JetourLinePlugin
 */
public class JetourLinePlugin implements FlutterPlugin, MethodCallHandler, EventChannel.StreamHandler {
    public static MapMsgListener mapMsgListener;

    private static final String CHANNEL_METHOD_MAP = "amap_flutter_plugin";
    private static final String CHANNEL_STREAM_MAP = "amap_flutter_plugin_stream";
    private MethodChannel channel;
    private EventChannel eventChannel;

    public static EventChannel.EventSink mEventSink = null;
    Context mComtext;

    @Override
    public void onAttachedToEngine(@NonNull FlutterPluginBinding flutterPluginBinding) {
        channel = new MethodChannel(flutterPluginBinding.getBinaryMessenger(), CHANNEL_METHOD_MAP);
        channel.setMethodCallHandler(this);

        eventChannel = new EventChannel(flutterPluginBinding.getBinaryMessenger(), CHANNEL_STREAM_MAP);
        eventChannel.setStreamHandler(this);

        MyViewFlutterPlugin.registerWith(flutterPluginBinding.getFlutterEngine());
        mComtext = flutterPluginBinding.getApplicationContext();
    }

    @Override
    public void onMethodCall(@NonNull MethodCall call, @NonNull Result result) {
        if (call.method.equals("buyLine")) {
            Uri uri = Uri.parse((String) call.arguments);
            Intent intent = new Intent(Intent.ACTION_VIEW, uri);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            mComtext.startActivity(intent);
        } else if (call.method.equals("showTxt")) {
            String msg = (String) call.arguments;
            IToast.showLong(mComtext, msg);
        } else if (call.method.equals("naviGo")) {
            MapUtil.goNavi(mComtext, (Map) call.arguments);
        }else if (call.method.equals("getMaps")) {
            List<String> maps = new ArrayList<>();
            if(checkAppInstalled(mComtext,"com.baidu.BaiduMap")){
                maps.add("百度地图");
            } if(checkAppInstalled(mComtext,"com.autonavi.minimap")){
                maps.add("高德地图");
            }if(checkAppInstalled(mComtext,"com.tencent.map")){
                maps.add("腾讯地图");
            }
            Map map = new HashMap();
            map.put("maps",maps);
            map.put("method","getMaps");
            mEventSink.success(map);
        }  else if (call.method.equals("startLocation")) {
            BaseLocationUtil locationUtil = new BaseLocationUtil(mComtext);
            locationUtil.setLocationListener(new BaseLocationUtil.LocationListener() {
                @Override
                public void onLocationChanged(Map locationMap) {
                    locationMap.put("method","startLocation");
                    mEventSink.success(locationMap);
                }
            });
            locationUtil.startLocation(false,mComtext);
        } else if (call.method.equals("set_map_key")) {
            Map<String, String> map = call.arguments();
            AMapLocationClient.setApiKey((String) map.get("android"));
        } else {
            if (mapMsgListener != null) {
                mapMsgListener.msgListener(call.method, call.arguments());
            }
        }
    }
    @Override
    public void onDetachedFromEngine(@NonNull FlutterPluginBinding binding) {
        channel.setMethodCallHandler(null);
        eventChannel.setStreamHandler(null);
    }


    @Override
    public void onListen(Object arguments, EventChannel.EventSink events) {
        mEventSink = events;
    }

    @Override
    public void onCancel(Object arguments) {

    }


    public interface MapMsgListener {
        void msgListener(String method, Object obj);
    }


    public static boolean checkAppInstalled(Context context, String pkgName) {
        if (TextUtils.isEmpty(pkgName)) {
            return false;
        }
        // 获取 PackageManager 对象
        PackageManager pm = context.getPackageManager();
        try {
            // 通过包名获取应用信息conte
            PackageInfo info = pm.getPackageInfo(pkgName, 0);
            // 如果 info 不为空，说明应用已安装
            if (info != null) {
                return true;
            } else {
                return false;
            }
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
            return false;
        }
    }

}
